#include<iostream>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
using namespace std;

int main()
{

  //int fd = open("log.txt",O_RDWR | O_CREAT | O_APPEND,0666);
  //if(fd<0)
  //{
  //  perror("open");
  //  exit(1);
  //}
  //const char* str = "hello linux\n";
  //write(fd,str,strlen(str));
  //char buf[1024];
  //lseek(fd,0,SEEK_SET);
  //while(1)
  //{
  //  ssize_t s = read(fd,buf,sizeof(buf));
  //  if(s>0)
  //  {
  //    printf("%s\n",buf);
  //  }
  //  else
  //  {
  //    break;
  //  }
  //}
  //close(fd);























  //输出信息到显示器
 printf("hello printf\n");

 const char* msg = "hello fwrite\n";
 fwrite(msg,strlen(msg),1,stdout);
 
 fprintf(stdout,"hello fprintf\n");

 FILE* fp = fopen("log.txt","w+");
 if(fp == nullptr)
 {
   perror("fopen:");
   return 1;
 }
 const char* str = "linux is so easy\n";
 char ptr[1024];
 fwrite(str,strlen(str),1,fp);
 fseek(fp,0,0);
  while(1)
  {
   ssize_t s = fread(ptr,1,sizeof str,fp);
   if(s>0)
   {
     ptr[s] = 0;
     printf("%s",ptr);
   }
   if(feof(fp))
   {
     break;
   }
  }
 fclose(fp);
  return 0;
}
